package com.org.zp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.ctrl.Constant;
import com.org.base.util.AESEncryptor;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.app.RecApp;
import com.org.zp.ctrl.ComMainActivity;
import com.org.zp.ctrl.UserMainActivity;
import com.org.zp.entity.UpdateInfo;
import com.org.zp.entity.login.Resume;

public class SplashActivity extends BaseActivity {

	private RecApp app;

	private boolean autoLogin;
	private String username;
	private String password;
	private String type;
	private ProgressDialog progressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_activity);

		if (!(ActivityTaskManager.getInstance().containsName("SplashActivity"))) {
			ActivityTaskManager.getInstance().putActivity("SplashActivity",
					this);
		}

		app = getMyApplication();

		SharedPreferences sp = getSharedPreferences(Constant.SHARE_PREFERENCE,
				Activity.MODE_PRIVATE);
		autoLogin = sp.getBoolean(Constant.REMENBER_PASSWORD, false);
		username = sp.getString(Constant.USERNAME, "");
		password = sp.getString(Constant.PASSWORD, "");
		type = sp.getString(Constant.TYPE, "");

		app.user.setName(username);
		try {
			app.user.setPsw(AESEncryptor.decrypt(AESEncryptor.KEY, password));
		} catch (Exception e) {
			e.printStackTrace();
		}

		app.user.setType(type);
		app.user.setAuto(autoLogin);

		progressDialog = ProgressDialog.show(SplashActivity.this, "请等待...",
				"正在初始化，请稍后...", true, true);

		new getSplashInfo().execute(URLHelper.mPrefix);

	}

	class getSplashInfo extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "getNewVersion");
			map.put("imei", app.getImei());
			map.put("phone", app.getPhone());
			map.put("imsi", app.getImsi());
			map.put("phoneType", app.getPhoneType());

			if (app.user.isAuto()) {
				map.put("username", app.user.getName());
				map.put("password", app.user.getPsw());
				map.put("userType", app.user.getType());
			}

			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			progressDialog.dismiss();

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				gotoMain();
			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				gotoMain();
			} else {
				parseJson(result);
			}

		}
	}

	private void parseJson(String str) {
		Log.e("TMG", "splash result = " + str);
		try {
			String succeed = new JSONObject(str).getString("succeed");
			if (succeed.equals("000")) {
				JSONObject json = new JSONObject(str);

				String update_data = json.getString("userData");

				JSONObject updatejson = new JSONObject(update_data);

				UpdateInfo up_info = new UpdateInfo();
				String apkurl = updatejson.getString("apkurl");
				up_info.setApkurl(apkurl == null ? "" : apkurl);
				String apkname = updatejson.getString("apkname");
				up_info.setApkname(apkname == null ? "" : apkname);
				String apkversion = updatejson.getString("apkversion");
				up_info.setApkversion(apkversion == null ? "" : apkversion);
				String apkremark = updatejson.getString("apkremark");
				up_info.setApkremark(apkremark == null ? "" : apkremark);
				String apksize = updatejson.getString("apksize");
				up_info.setApksize(apksize == null ? "" : apksize);

				app.user.setUp_info(up_info);

				if (app.user.isAuto()) {
					String user_data = json.getString("loginMap");

					JSONObject autojson = new JSONObject(user_data);
					String autosucceed = autojson.getString("succeed");

					if (autosucceed.equals("000")) {

						app.user.setuID(autojson.getString("uid"));
						app.user.setType(autojson.getString("utype"));

					}

					String resume_data = json.getString("resumeList");
					JSONArray resume_list = new JSONArray(resume_data);

					ArrayList<Resume> resList = new ArrayList<Resume>();

					for (int i = 0; i < resume_list.length(); i++) {
						Resume info = new Resume();
						JSONObject log = resume_list.getJSONObject(i);
						info.setResume_id(log.getString("id"));
						info.setResume_title(log.getString("title"));
						info.setIsdefaultflag(log.getString("audit_cn"));
						info.setResume_addtime(log.getString("addtime"));
						info.setShow_count(log.getString("click"));
						info.setResume_talent_cn(log.getString("talent_cn"));
						resList.add(info);
					}

					app.user.setResume(resList);
				}

				gotoMain();

			} else {
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void gotoMain() {
		
		if (app.user.getType().equals("1")) {
			Intent intent = new Intent(SplashActivity.this,
					ComMainActivity.class);
			startActivity(intent);
		} else {
			Intent intent = new Intent(SplashActivity.this,
					UserMainActivity.class);
			startActivity(intent);
		}

		ActivityTaskManager.getInstance().removeActivity("SplashActivity");
	}

}
